package com.youdelu.stock.dao.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdelu.stock.dao.IStockDIService;
import com.youdelu.stock.bean.GpData;
import com.youdelu.stock.dao.actions.QuoteList.task.QuListTask;
import com.youdelu.stock.utils.HttpsUtils;
import com.youdelu.stock.bean.StockBean;
import com.youdelu.stock.bean.StockDayInfo;
import com.youdelu.stock.mapper.StockMapper;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class StockService extends ServiceImpl<StockMapper, GpData> implements IStockDIService {

    private static final Logger logger = LoggerFactory.getLogger(StockService.class);
    public static ExecutorService executor = Executors.newFixedThreadPool(50);
    @Resource
    StockMapper stockMapper;

    @Override
    public void createStockInfoTable(String stockNum) {
        stockMapper.createStockDayInfoTable("stockdayinfo_" + stockNum);
        stockMapper.createStockWeekInfoTable("stockweekinfo_" + stockNum);
        stockMapper.createStockMonthInfoTable("stockmonthinfo_" + stockNum);
        stockMapper.createStockTimeInfoTable("stocktimeinfo_" + stockNum);
    }


    @Override
    public void insertStockList(List<GpData> stockBeanList) {
        stockMapper.insertStockList(stockBeanList);
    }

    @Override
    public List<GpData> selectStockList(@Param("key") String key, @Param("beginCount") Long beginCount, @Param("pageCount") Long pageCount) {
//        TableQueryBean miniuiTablePageQuery = MiniuiUtil.getMiniuiTablePageQuery(miniuiTableQueryBean);
        return stockMapper.selectStockList(key, beginCount, pageCount);
    }


    @Override
    public int getStockCount(String key, Long beginCount, Long pageCount) {
        return stockMapper.getStockCount(key, beginCount, pageCount);
    }

    @Override
    public List<GpData> selectTotalStockList() {
        return stockMapper.selectTotalStockList();
    }

    @Override
    public StockBean getStockInfoById(String stockId) {
        return stockMapper.getStockInfoById(stockId);
    }

    @Override
    public List<GpData> getNoExistStockList(List<GpData> stockBeanList) {
        List<GpData> stockList = selectTotalStockList();
        List<GpData> newStock = new ArrayList<>();
        for (int i = 0; i < stockBeanList.size(); i++) {
            if (!stockList.contains(stockBeanList.get(i))) {
                newStock.add(stockBeanList.get(i));
            }
        }
        return newStock;
    }


    public JSONObject getStockShare(String stockCode) {
        System.out.println("正在获取股票编号：" + stockCode);
        String url = "http://query.sse.com.cn/commonQuery.do";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isPagination", "false");
        param.put("sqlId", "COMMON_SSE_CP_GPLB_GPGK_GBJG_C");
        param.put("companyCode", stockCode);
        String sendResult = HttpsUtils.doGetString(url, param);
        JSONArray result = null;
        try {
            result = JSONObject.parseObject(sendResult).getJSONArray("result");
        } catch (Exception e) {
            logger.error("com.alibaba.fastjson.JSONException: scan null error:{}" + e);
        }

        if (result == null || result.size() == 0) {
            return new JSONObject();
        }
        return result.getJSONObject(0);
    }


    @Override
    public List<GpData>  getStockListByScript() {
        QuListTask  quListTask = new QuListTask();
        return quListTask.getGpData();
    }

    public Double objectToBigDecimal(Object o) {
        try {
            return Double.valueOf(String.valueOf(o));
        } catch (NumberFormatException e) {
            return Double.valueOf(0);
        }


    }


    @Override
    public void updateStock(GpData stockBean) {
        stockMapper.updateById(stockBean);
    }

    @Override
    public GpData getStockInfo(GpData stockBean, List<StockDayInfo> stockdayinfoList) {
        GpData currentStockBean = new GpData();
        BeanUtil.copyProperties(stockBean, currentStockBean);
//        int stockdayinfoListSize = stockdayinfoList.size();
//        StockDayInfo currentStockdayinfo = stockdayinfoList.get(stockdayinfoListSize - 1);
//        StockDayInfo preStockdayinfo = stockdayinfoList.get(stockdayinfoListSize - 1 - 1);
//        StockDayInfo pre3Stockdayinfo = new StockDayInfo();
//        StockDayInfo pre5Stockdayinfo = new StockDayInfo();
//        if (stockdayinfoListSize >= 4) {
//            pre3Stockdayinfo = stockdayinfoList.get(stockdayinfoListSize - 1 - 3);
//        }
//        if (stockdayinfoListSize >= 6) {
//            pre5Stockdayinfo = stockdayinfoList.get(stockdayinfoListSize - 1 - 5);
//        }
//
//        double currentClosePrise = currentStockdayinfo.getClose();
//        double currentVolume = currentStockdayinfo.getVolume();
//        double currentTotalFlowShares = stockBean.getTotalFlowShares();
//        double currentHigh = currentStockdayinfo.getHigh();
//        double currentLow = currentStockdayinfo.getLow();
//        double preClosePrise = preStockdayinfo.getClose();
//        double pre3ClosePrise = pre3Stockdayinfo.getClose();
//        double pre5ClosePrise = pre5Stockdayinfo.getClose();
//        double upDownRange = 0;
//        double upDownRange3 = 0;
//        double upDownRange5 = 0;
//        double turnOverrate = 0;
//        if (currentTotalFlowShares == 0) {
//            logger.error("currentTotalFlowShares is zero, stockBean : {}", JSON.toJSONString(stockBean));
//        } else {
//            turnOverrate = currentVolume * 100 / currentTotalFlowShares;
//        }
//
//        double upDownPrices = currentClosePrise - preClosePrise;
//        String newDate = currentStockdayinfo.getDate();
//
//        double amplitude = 0;
//        if (currentClosePrise - preClosePrise != 0) {
//            if (preClosePrise == 0) {
//                logger.error("preClosePrise is zero, stockBean : {}", JSON.toJSONString(stockBean));
//            } else {
//                amplitude = (currentHigh - currentLow) / preClosePrise;
//                upDownRange = (currentClosePrise - preClosePrise) / preClosePrise;
//            }
//
//        }
//        if (currentClosePrise - pre3ClosePrise != 0) {
//            if (pre3ClosePrise == 0) {
//                logger.error("pre3ClosePrise is zero, stockBean : {}", JSON.toJSONString(stockBean));
//            } else {
//                upDownRange3 = (currentClosePrise - pre3ClosePrise) / pre3ClosePrise;
//            }
//
//        }
//        if (currentClosePrise - pre5ClosePrise != 0) {
//            if (pre5ClosePrise == 0) {
//                logger.error("pre5ClosePrise is zero, stockBean : {}", JSON.toJSONString(stockBean));
//            } else {
//                upDownRange5 = (currentClosePrise - pre5ClosePrise) / pre5ClosePrise;
//            }
//
//        }
//
//        currentStockBean.setUpDownRange(upDownRange);
//        currentStockBean.setUpDownRange3(upDownRange3);
//        currentStockBean.setUpDownRange5(upDownRange5);
//        currentStockBean.setTurnOverrate(turnOverrate);
//        currentStockBean.setUpDownPrices(upDownPrices);
//        currentStockBean.setOpen(currentStockdayinfo.getOpen());
//        currentStockBean.setClose(currentStockdayinfo.getClose());
//        currentStockBean.setHigh(currentStockdayinfo.getHigh());
//        currentStockBean.setLow(currentStockdayinfo.getLow());
//        currentStockBean.setPreClose(preClosePrise);
//        currentStockBean.setVolume(currentStockdayinfo.getVolume());
//        currentStockBean.setAmount(currentStockdayinfo.getAmount());
//        currentStockBean.setAmplitude(amplitude);
//        currentStockBean.setTotalMarketValue(stockBean.getTotalShares() * currentClosePrise);
//        currentStockBean.setFlowMarketValue(stockBean.getTotalFlowShares() * currentClosePrise);
//        currentStockBean.setStockId(stockBean.getStockId());
//        currentStockBean.setUpdateDate(newDate);

        return currentStockBean;
    }

}
